define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'agent_order/index' + location.search,
                    add_url: 'agent_order/add',
                    edit_url: 'agent_order/edit',
                    del_url: 'agent_order/del',
                    multi_url: 'agent_order/multi',
                    table: 'agent_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('User_id'), visible: false},
                        {field: 'user.nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'recommend.nickname', title: __('推荐人'), operate: 'LIKE'},
                        {field: 'level', title: __('Level'), searchList: Config.levelList, visible: false},
                        {field: 'level_text', title: __('Level'), operate: false},
                        {field: 'amount', title: __('Amount'), operate: false},
                        {field: 'status', title: __('Status'), searchList: Config.statusList, visible: false},
                        {field: 'status_text', title: __('Status'), operate: false},
                        // {field: 'pay_type', title: __('Pay_type')},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                         {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons: [
                                 {
                                    name: 'done', 
                                    text: '更改为已支付', 
                                    title: '更改为已支付', 
                                    icon: '', 
                                    classname: 'btn btn-xs btn-danger btn-ajax', 
                                    confirm: '确认更改为已支付吗？', 
                                    url: 'agent_order/paySuccess',
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                    }
                                }
                            ],
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                if(row.status != 0) {
                                    $(table).data("operate-done", null);
                                }
                                $(table).data("operate-del", null);
                                $(table).data("operate-edit", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});